/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.enac.geoapp.wims.model;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import edu.enac.geoapp.wims.exception.WimsException;

/**
 * Abstract class encapsulating the common fields of the various product types
 * @author mathieu
 *
 */
public abstract class Product {
	private String swpId;
	private int confidenceLevel;
	private int intensityLevel;
	
	private PolygonBuilder polygon = new PolygonBuilder();
	
	@SuppressWarnings("rawtypes")
	public void initField(String fieldName, String value) {
		try {
			Method m = findMethod(fieldName);
			Class parameterType = m.getParameterTypes()[0];
			if(parameterType == boolean.class)
				m.invoke(this, Boolean.parseBoolean(value));
			if(parameterType == String.class)
				m.invoke(this, value);
			if(parameterType == int.class)
				m.invoke(this, Integer.parseInt(value));
			if(parameterType == float.class) 
				m.invoke(this, Float.parseFloat(value));
			if(parameterType == Point.class)
				m.invoke(this, ProductBuilderUtilities.getPoint(value));
			if(parameterType == Trend.class)
				m.invoke(this, ProductBuilderUtilities.getTrend(value));
			if(parameterType == LayerType.class)
				m.invoke(this, ProductBuilderUtilities.getLayerType(value));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WimsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Method findMethod(String fieldName) {
		Method methodList[] = this.getClass().getDeclaredMethods();
		for(Method m: methodList) {
			if(m.getName().startsWith("set") && m.getName().toLowerCase().contains(fieldName.toLowerCase())) {
				return m;
			}
		}
		return null;
	}

	
	/**
	 * @return the swpId
	 */
	public String getSwpId() {
		return swpId;
	}
	/**
	 * @param swpId the swpId to set
	 */
	public void setSwpId(String swpId) {
		this.swpId = swpId;
	}
	/**
	 * @return the confidenceLevel
	 */
	public int getConfidenceLevel() {
		return confidenceLevel;
	}
	/**
	 * @param confidenceLevel the confidenceLevel to set
	 */
	public void setConfidenceLevel(int confidenceLevel) {
		this.confidenceLevel = confidenceLevel;
	}
	/**
	 * @return the geometry
	 */
	public PolygonBuilder getPolygon() {
		return polygon;
	}
	/**
	 * @param polygon the geometry to set
	 */
	public void setPolygon(PolygonBuilder polygon) {
		this.polygon = polygon;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ProductType [swpId=" + swpId + ", confidenceLevel="
				+ confidenceLevel + ", geometry=" + polygon + "]";
	}

	public void setIntensityLevel(int intensityLevel) {
		this.intensityLevel = intensityLevel;
	}

	public int getIntensityLevel() {
		return intensityLevel;
	}
	
	
	
}
